﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;


namespace LiveProjectManager.DDD.Base
{
    public abstract class Service<T> where T : Domain
    {
        protected static EntityContext _context = EntityContext.Instance;
        protected DbSet<T> _repository;


        protected Service() {
            _repository = (DbSet<T>)_context.GetType()
                .GetProperty(typeof(T).Name + "Repository")
                .GetValue(_context, null);
            //throw new Exception("Unable to find '" + myDomainType + "' repository.");
        }


        abstract public ICollection<T> getAll(int paginateBy = 0, int page = 1);


        public bool exists(long myId)
        {
            return (getById(myId) != null);
        }


        abstract public T getById(long myId);


        abstract public bool remove(long myId, bool autoSave = true);


        public static ICollection<Dictionary<string, object>> toDict(ICollection<T> myDomains)
        {
            ICollection<Dictionary<string, object>> myExportedDomains = new List<Dictionary<string, object>>();
            foreach (T myDomain in myDomains)
                myExportedDomains.Add(myDomain.toDict());
            return myExportedDomains;
        }
    }
}